package com.czl.module_storehouse.activity.remand.details;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.czl.base.data.bean.tengyun.BorrowBean;
import com.czl.base.data.bean.tengyun.ReceiveBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.RemandInfoAdapter;
import com.czl.module_storehouse.bean.RemandBean;
import com.czl.module_storehouse.databinding.FooterCommonRemarkBinding;
import com.czl.module_storehouse.databinding.HeaderRemandReceiveBinding;
import com.czl.module_storehouse.databinding.LayoutRecyclerViewBinding;
import com.czl.module_storehouse.event.RemandEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author xubin
 */
public class RemandReceiveDetailsActivity extends BaseActivity<LayoutRecyclerViewBinding> {

    private HeaderRemandReceiveBinding mHeaderBinding;
    private RemandInfoAdapter mAdapter;

    @Override
    protected LayoutRecyclerViewBinding getBinding(LayoutInflater layoutInflater) {
        return LayoutRecyclerViewBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("领用详情");
        mHeaderBinding = HeaderRemandReceiveBinding.inflate(getLayoutInflater());
        mAdapter = new RemandInfoAdapter(R.layout.item_remand_info, getType());
        binding.recyclerView.setAdapter(mAdapter);
        mAdapter.addHeaderView(mHeaderBinding.getRoot());
        EventBus.getDefault().register(this);
    }

    private int getType() {
        String type = getIntent().getStringExtra("type");
        if ("borrow".equals(type)) {
            return Constants.Tag.TAG_BORROW;
        } else {
            return Constants.Tag.TAG_RECEIVE;
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(RemandEvent event) {
        if (event != null && event.getRemandBean() != null) {
            RemandBean remandBean = event.getRemandBean();
            ReceiveBean receiveBean = remandBean.getReceiveBean();
            BorrowBean borrowBean = remandBean.getBorrowBean();
            FooterCommonRemarkBinding remarkBinding = FooterCommonRemarkBinding.inflate(getLayoutInflater());
            remarkBinding.etRemark.setEnabled(false);
            mAdapter.addFooterView(remarkBinding.getRoot());
            if (receiveBean != null) {
                remarkBinding.etRemark.setText(receiveBean.getReturnComment());
                mAdapter.addData(receiveBean.getSortList());
            } else if (borrowBean != null) {
                remarkBinding.etRemark.setText(borrowBean.getReturnComment());
                mAdapter.addData(borrowBean.getSortList());
            }
            initHeaderView(remandBean);
        }
    }

    @SuppressLint("SetTextI18n")
    private void initHeaderView(RemandBean remandBean) {
        if (remandBean == null || remandBean.getReceiveBean() == null) {
            return;
        }
        ReceiveBean receiveBean = remandBean.getReceiveBean();
        mHeaderBinding.clHeaderInfo.tvHeaderTitle.setText(receiveBean.getReceiveName());
        mHeaderBinding.clHeaderInfo.tvHeaderDate.setText(receiveBean.getReceiveDate());
        mHeaderBinding.clHeaderInfo.tvHeaderCode.setText(receiveBean.getReceiveCode("领用单："));
        mHeaderBinding.tvHeaderDepartment.setText("领用部门：" + receiveBean.getReceivePersonDepartment());
        mHeaderBinding.tvHeaderDesc.setText("领用原因：" + receiveBean.getApplyComment());
        mHeaderBinding.tvReturnDate.setText("实还日期：" + receiveBean.getReturnDateStr8());
        mHeaderBinding.tvReturnDate.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
    }
}
